package com.controller.ssjf;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.base.Const;
import com.controller.base.BaseController;
import com.entity.Column;
import com.entity.News;
import com.service.ColumnService;
import com.service.NewsService;

@Controller
@RequestMapping("/ssjf")
public class SSJFNewsController extends BaseController<Object>{

	@Autowired
	private NewsService newsService;
	@Autowired
	private ColumnService columnService;
	
	/**
	 * 新闻资讯页面跳转
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("page/news.html")
	public String news(Model model)throws Exception{
		//查出新闻资讯的栏目
		List<Column> cols = columnService.findNews(new Column());
		model.addAttribute("cols", cols);
		return "ssjf/pages/news";
	}
	
	/**
	 * 最新政策菜单栏页面跳转
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("page/news2.html")
	public String news2(Model model, @RequestParam(value="id") String id,
			Integer val)throws Exception{
		//获取栏目
		if(id != null && !id.equals("") && (id.equals("02") || id.equals("03"))){
			System.out.println(id+","+val);
			Column col = new Column();
			if(id.equals("03")){
				col.setId(id);
			}else{
				col.setHigherColumnId(id);
				if(val != null){
					switch (val) {
					case 1:
						col.setColumnName("国家政策");
						break;
					case 2:
						col.setColumnName("本地政策");
						break;
					case 3:
						col.setColumnName("热门项目");
						break;
					case 4:
						col.setColumnName("金融政策");
						break;
					case 5:
						col.setColumnName("金融知识");
						break;
					default:
						return "ssjf/index";
					}
				}
			}
			col = columnService.find(col).get(0);
			model.addAttribute("colName", col.getColumnName());
			//获取栏目下信息
			News news = new News();
			news.setColumnId(col.getId());
			List<News> newses = newsService.find(news);
			model.addAttribute("newses", newses);
		}
		return "ssjf/pages/news2";
	}
	
	/**
	 * 新闻资讯页面信息获取
	 * @param news
	 * @param page
	 * @return
	 */
	@RequestMapping("/news/info.dat")
	@ResponseBody
	public AjaxRes info(News news, Page<News> page){
		AjaxRes res = new AjaxRes();
		Page<News> newses = newsService.findByPage(news, page);
		res.setSucceed(newses, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("page/news/detail.html")
	public String detail(Model model,@RequestParam(value="prevPage")String prevPage,
			News news){
		if(prevPage != null) getRequest().getSession().setAttribute("prevPage", prevPage);
		news = newsService.find(news).get(0);
		model.addAttribute("news", news);
		return "ssjf/pages/news3";
	}
}
